package edu.gu.derp.beans.register;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import edu.gu.derp.model.Account;
import edu.gu.derp.model.Database;
import edu.gu.derp.model.IController;
import edu.gu.derp.model.Mail;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.mail.MessagingException;

/**
 *
 * @author jensc
 */
@ManagedBean(name = "RegisterControllerBean")
@RequestScoped
public class RegisterControllerBean {
    
    private final int CONSTRAINT_NAME_MIN_LENGTH = 3;
    private final int CONSTRAINT_NAME_MAX_LENGTH = 20;
    private final int CONSTRAINT_PASSWORD_MIN_LENGTH = 5;
    private final int CONSTRAINT_PASSWORD_MAX_LENGTH = 20;
    private final int CONSTRAINT_EMAIL_NULL = 0;
   
    
    private final String ERROR_EMAIL_NOT_VALID = "The inputed email is not of valid form";
    private final String ERROR_NAME_TO_SHORT = "Name is less than " + CONSTRAINT_NAME_MIN_LENGTH + "characters";
    private final String ERROR_NAME_TO_LONG = "Name is more than " + CONSTRAINT_NAME_MAX_LENGTH +" characters";
    private final String ERROR_PASSWORD_TO_SHORT = "The password is too short - it should be at least " + CONSTRAINT_PASSWORD_MIN_LENGTH + "characters long";
    private final String ERROR_PASSWORD_TO_LONG = "The password is too long - it should be no more than " + CONSTRAINT_PASSWORD_MAX_LENGTH + " characters long";
    private final String ERROR_PASSWORD_DONT_MATCH = "The passwords entered don't match each other";
    private final String ERROR_ACCOUNT_EXISTS = "The account name already exists, try a new name";

    
    @ManagedProperty(value = "#{RegisterBackingBean.name}")
    private String name;
    @ManagedProperty(value = "#{RegisterBackingBean.email}")
    private String email;
    @ManagedProperty(value = "#{RegisterBackingBean.password}")
    private String password;
    @ManagedProperty(value = "#{RegisterBackingBean.confirmPassword}")
    private String confirmPassword;

    public String confirmRegistration() {
        FacesContext context = FacesContext.getCurrentInstance();
              
        IController controller = Database.getController();
        
         
        if(controller.accountExists(name)){
            FacesMessage messageName = new FacesMessage();
            messageName.setSummary(ERROR_ACCOUNT_EXISTS);
            messageName.setDetail(ERROR_ACCOUNT_EXISTS);
            messageName.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formRegister:regName", messageName);
        } else if (name.length() <= CONSTRAINT_NAME_MAX_LENGTH && name.length() >= CONSTRAINT_NAME_MIN_LENGTH
                   && password.length() <= CONSTRAINT_PASSWORD_MAX_LENGTH && password.length() >= CONSTRAINT_PASSWORD_MIN_LENGTH 
                   && password.equals(confirmPassword)
                   && email.length() != CONSTRAINT_EMAIL_NULL && email.contains("@")){           
            Account account = new Account(name, password, email);
            controller.addAccount(account);
            try {
                Mail.sendWelcomeMail(account);
            } catch (MessagingException ex) {
                Logger.getLogger(RegisterControllerBean.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            return "login.jsf";
        }


        if (name.length() < CONSTRAINT_NAME_MIN_LENGTH) {
            FacesMessage messageName = new FacesMessage();
            messageName.setSummary(ERROR_NAME_TO_SHORT);
            messageName.setDetail(ERROR_NAME_TO_SHORT);
            messageName.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formRegister:regName", messageName);
        } else if (name.length() > CONSTRAINT_NAME_MAX_LENGTH) {
            FacesMessage messageName = new FacesMessage();
            messageName.setSummary(ERROR_NAME_TO_LONG);
            messageName.setDetail(ERROR_NAME_TO_LONG);
            messageName.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formRegister:regName", messageName);
        }
        
        if (email.length() == CONSTRAINT_EMAIL_NULL || !email.contains("@")){
            FacesMessage message = new FacesMessage();
            message.setSummary(ERROR_EMAIL_NOT_VALID);
            message.setDetail(ERROR_EMAIL_NOT_VALID);
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formRegister:email", message);
        }

        if (password.length() < CONSTRAINT_PASSWORD_MIN_LENGTH) {
            FacesMessage message = new FacesMessage();
            message.setSummary(ERROR_PASSWORD_TO_SHORT);
            message.setDetail(ERROR_PASSWORD_TO_SHORT);
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formRegister:password", message);
        } else if (password.length() > CONSTRAINT_PASSWORD_MAX_LENGTH) {
            FacesMessage message = new FacesMessage();
            message.setSummary(ERROR_PASSWORD_TO_LONG);
            message.setDetail(ERROR_PASSWORD_TO_LONG);
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formRegister:password", message);
        }

        if (!password.equals(confirmPassword)) {
            FacesMessage message = new FacesMessage();
            message.setSummary(ERROR_PASSWORD_DONT_MATCH);
            message.setDetail(ERROR_PASSWORD_DONT_MATCH);
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage("formRegister:confirmPassword", message);
        } 
        
        return "register.jsf";
        
    }
    
    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
